package dth.utils;
import java.io.IOException;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.context.ExternalContext;

import org.primefaces.context.RequestContext;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import dth.bean.LoginBean;
import dth.entity.Account;
import dth.entity.User;
public class DthUtil {
	public static void updatePageElement(String id){
		RequestContext rc = RequestContext.getCurrentInstance();
		rc.update(id);
	}
	public static HttpServletRequest getRequest()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		ExternalContext externalContext=context.getExternalContext();
		return (HttpServletRequest) externalContext.getRequest();
	}
	public static void showMessage(String title,String detail)
	{
		FacesContext context=FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(title,detail));
	}
	public static void setSession(String name,Object object)
	{
		FacesContext context = FacesContext.getCurrentInstance();  
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
		
		HttpSession httpSession = request.getSession(false);  
		httpSession.setAttribute(name, object);
	}
	public static Object getSession(String name)
	{
		FacesContext context = FacesContext.getCurrentInstance();  
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
		HttpSession httpSession = request.getSession(false);  
		Object reqdObj = (Object) httpSession.getAttribute(name);  
		return reqdObj;
	}
	public static Account getAccountDTH()
	{
		return (Account) getSession(DthSessionConstant.DTH_USER);
	}
	public static void navigateTo(String url){
		FacesContext context = FacesContext.getCurrentInstance(); 
		try {
			context.getExternalContext().redirect(url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void removeSession(String name)
	{
		FacesContext context = FacesContext.getCurrentInstance();  
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
		HttpSession httpSession = request.getSession(false);  
		httpSession.removeAttribute(name);
	}
	public static String getFormatedNumber(double num){
		NumberFormat format = new DecimalFormat("##,###,###");
		return format.format(num);
	}
	public static boolean isVaildEmail(String s){
		String PATTERN = 
				"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		Pattern pattern=Pattern.compile(PATTERN);
		Matcher matcher=pattern.matcher(s);
		return matcher.find();
	}
	
	public static boolean isVaildMoneyNumber(String s){
		/*
		 * phai la so nguyen duong
		 */
		String PATTERN = "^[1-9][0-9]*$";
		Pattern pattern=Pattern.compile(PATTERN);
		Matcher matcher=pattern.matcher(s);
		return matcher.find();
	}
	public static boolean isVaildPhoneNumber(String s){
		/*/
		 *10 or 11 so
		 */
		String PATTERN = "^\\d{10}$|^\\d{11}$";
		Pattern pattern=Pattern.compile(PATTERN);
		Matcher matcher=pattern.matcher(s);
		return matcher.find();
	}
	public static java.sql.Date convertToSqlDate(java.util.Date date){
		return new java.sql.Date(date.getTime());
	}
	public static void showReport(String path) throws SQLException{
		Connection con=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dth2","root","123");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
			System.out.println("try");
			String reporte = extContext.getRealPath(path);
			
			JasperReport jasperreport = JasperCompileManager.compileReport(reporte);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperreport, null,con);
			JasperViewer.viewReport(jasperPrint,false);
			con.close();

		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	public static String getRequestURL(){
		HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		return origRequest.getRequestURL().toString();
	}
	public static String convertToMD5(String arg) {
		String hashed_key = "";

		try {
			byte[] intext = arg.getBytes();
			StringBuffer sb = new StringBuffer();
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] md5rslt = md5.digest(intext);
			for (int i = 0; i < md5rslt.length; i++) {
				sb.append(String.format("%02x", 0xff & md5rslt[i]));
			}
			hashed_key = sb.toString();
		} catch (Exception e2) {
			System.out.println(e2);
		}
		return hashed_key;
	}
	/*
	 * get ket language
	 * 
	 */
	public static String getStringByKey(String key){
		ResourceBundle bundle = ResourceBundle.getBundle("resource.dth", FacesContext.getCurrentInstance().getViewRoot().getLocale());
		return bundle.getString(key);
	}
	public static void showMessageSuccess(){
		showMessage(getStringByKey("bean_message"), getStringByKey("bean_message_success"));
	}
	public static void showMessageError(){
		showMessage(getStringByKey("bean_message"), getStringByKey("bean_message_error"));
	}
}
